package com.wu.controller;

import com.wu.response.CommonReturnType;
import com.wu.service.CollectArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.wu.constant.Constant.COLLECT;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-05-07 16:25
 */
@RestController
@Api(tags = "文章收藏")
public class CollectController {
    @Autowired
    private CollectArticleService collectArticleService;

    /**
     * 添加收藏或取消收藏
     *
     * @param articleId
     * @return
     */
    @ApiOperation(value = "添加收藏或取消收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章ID", required = true, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/addOrDeleteCollect", method = RequestMethod.POST)
    public CommonReturnType tokenAddOrDeleteCollect(@RequestParam(name = "articleId") String articleId) {
        HashMap<String, Boolean> map = new HashMap<>();
        map.put(COLLECT, collectArticleService.addOrDeleteCollect(articleId));
        return CommonReturnType.create(map);
    }

    /**
     * 获取用户收藏
     *
     * @return
     */
    @ApiOperation(value = "获取用户收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "curPage", value = "当前页数", required = true, dataTypeClass = Integer.class)
    })
    @RequestMapping(value = "/getUserCollect", method = RequestMethod.POST)
    public CommonReturnType getUserCollect(@RequestParam("userId") String userId,
                                           @RequestParam("curPage") Integer curPage) {
        Map<String, Object> map = collectArticleService.getCollectArticleByUserId(userId, curPage);
        return CommonReturnType.create(map);
    }
}
